#include <types.h>
#include "APIC.h"

#ifdef DEBUG
#include <stdio.h>
#endif


void write_ioapic_reg(uint_32 ioapic_base, const uint_8 offset, const uint_32 val)
{
  *(uint_32 *)ioapic_base = offset;	/* 设置 IOREGSEL */
  *(uint_32 *)(ioapic_base + 0x10) = val;	/* 设置 IOWIN */
}


uint_32 read_ioapic_reg(uint_32 ioapic_base, const uint_8 offset)
{
  *(uint_32 *)ioapic_base = offset;
  return *(uint_32 *)(ioapic_base + 0x10);
}




void init_ioapic()
{
  uint_32  max_rdt_entry,reg;		/* 重定向表的最大数目 IOAPIC VERSION　寄存器的 23：16 位 */

  printf("Init IO APIC\n");
  reg = read_ioapic_reg(IOAPIC_BASE, 0x01);
  max_rdt_entry = (reg>>16)&0x000000ff;
#ifdef DEBUG
  printf("Maximu Redirection Entry: %X\n", max_rdt_entry);
#endif

  /* IRQ1 键盘中断33 */
  write_ioapic_reg(IOAPIC_BASE, 0x12, 0x21);
  write_ioapic_reg(IOAPIC_BASE, 0x13, 0x0);

}

